<?php

namespace app\common\model\merchant;


use app\common\model\BaseModel;
use Exception;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;


/**
 * 商户商品分类
 * Class ProductClass
 * @package app\common\model
 */
class ProductClassModel extends BaseModel
{
    //
    protected $name = 'product_class';

    // 设置字段信息
    protected $schema = [
        'id' => 'int',        // 分类ID
        'mer_id' => 'string',        // 商户ID
        'title' => 'string',        // 商品名称
        'pid' => 'int',        // 父节点ID
        'child' => 'int',        // 子节点数
        'depth' => 'int',        // 深度
        'sort' => 'int',        // 排序,越大越前
        'op_user' => 'int',        // 操作人
        'create_at' => 'datetime',        // 创建时间
        'update_at' => 'datetime',        // 更新时间
    ];

    /**
     * 列表
     * @param $where | 条件
     * @return array|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function classList($where,$page = 10): ?array
    {

        $data = $this->db()->where($where ?? [])
            ->field('id,pid,title,sort,child')
            ->order('id desc')
            ->limit($page)
            ->select();

        //$data =  Extend::Arrays()->toTree($data,'id','pid');
        return  $data;
    }



    /**
     * 修改、添加
     * @param $data
     * @param int $id
     * @return int
     * @throws DbException|Exception
     */
    public function save($data, int $id = 0): int
    {
        $where[] = ['mer_id', '=', $data['mer_id']];
        if ($id !== 0) {
            $where[] = ['id', '<>', $id];
        }
        $where[] = ['title', '=', $data['title']];

        //分类不能重复
        $cont = $this->db()->where($where)->count('id');
        if ($cont > 0) {
            throw new Exception('分类名称已经存在', API_ERROR);
        }

        return $this->treeAddEdit($data);

        /*unset($data['id']);
        if ($id !== 0) {
          return  $this->db()->where('id', $id)->update($data);
        }*/

        //$this->db()->insert($data);
        /* if ($data['pid'] !== 0) {
             $this->db()->where('id', $data['pid'])->inc('child')->update();
         }*/

        //return true;

    }


    /**
     * 删除分类
     * @param $id
     * @return int
     * @throws DbException
     */
    public function del($id,$mer_id): int
    {
        $pid = $this->db()->where('id', $id)->where('mer_id' , $mer_id)
            ->value('pid');

        $count = $this->db()->where('pid', $id)->where('mer_id' , $mer_id)
            ->count('id');
        if ($count > 0) {
            throw new Exception('已有子分类无法删除');
        }

        $proNum = $this->db('product')->where('class_id', $id)->where('mer_id' , $mer_id)
            ->count('id');
        if ($proNum > 0) {
            throw  new Exception('已有商品绑定分类无法删除');
        }

        if ($pid !== 0) {
            $this->db()->where('id', $pid)->where('mer_id' , $mer_id)
                ->dec('child')->update();
        }

        return $this->db()->where('id', $id)->where('mer_id' , $mer_id)->delete();

    }


    /**
     * 删除分类
     * @param $id
     * @throws DbException
     * @throws Exception ;
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function info($id)
    {
        $data = $this->db()->where('id', $id)->field('id,pid,sort,title')->find();
        if (!$data) {
            throw new \think\Exception('未找到分类', API_ERROR);
        }
        return $data;
    }

}
